package br.com.mmm.javacenter.framework.view.displaytag;

import javax.servlet.jsp.PageContext;

import org.displaytag.decorator.DisplaytagColumnDecorator;
import org.displaytag.exception.DecoratorException;
import org.displaytag.properties.MediaTypeEnum;

import br.com.mmm.javacenter.framework.entity.EntidadeBase;
import br.com.mmm.javacenter.framework.view.formatters.Formatter;
import br.com.mmm.javacenter.framework.view.formatters.FormatterFactory;

public class GenericDataDecorator implements DisplaytagColumnDecorator {

	@Override
	public Object decorate(Object arg0, PageContext arg1, MediaTypeEnum arg2) throws DecoratorException {
		if(arg0==null){
			return "";
		}else if(EntidadeBase.class.isAssignableFrom(arg0.getClass())){
			return FormatterFactory.getFormatter(EntidadeBase.class).format(arg0);
		}else{
			Formatter formatter = FormatterFactory.getFormatter(arg0.getClass());
			if(formatter!=null){
				return formatter.format(arg0);
			}else{
				return arg0;
			}
		}
	}
	
}
